use anyhow::{Ok, Result};
use clap::Parser;
use reqwest::Url;

// 定义 HTTPie 的 CLI 的主入口，它包含若干个子命令
// 下面 /// 的注释是文档，clap 会将其作为 CLI 的帮助

/// 使用原生rust实现的httpie
#[derive(Parser, Debug)]
#[clap(version = "1.0", author = "ezv <ezv@cock.li>")]
struct Opts {
    #[clap(subcommand)]
    subcmd: SubCommand,
}

#[derive(Parser, Debug)]
enum SubCommand {
    Get(Get),
    Post(Post),
}

fn parse_url(s: &str) -> Result<String> {
    let _url: Url = s.parse()?;
    Ok(s.into())
}

/// 发起 GET 请求
#[derive(Parser, Debug)]
struct Get {
    #[clap(parse(try_from = parse_url))]
    url: String,
}

/// 发起 POST 请求
#[derive(Parser, Debug)]
struct Post {
    url: String,
    body: Vec<String>,
}
fn main() {
    let opts = Opts::parse();
    println!("{:?}", opts);
}
